package edu.hebtu.controller;

import edu.hebtu.mapper.impl.AdminMapper;
import edu.hebtu.mapper.impl.UserMapper;
import edu.hebtu.mapper.impl.SubjectMapper;
import edu.hebtu.pojo.Admin;
import edu.hebtu.pojo.Subject;
import edu.hebtu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    private final AdminMapper adminMapper;
    private final UserMapper userMapper;
    private final SubjectMapper subjectMapper;

    @Autowired
    public LoginController(AdminMapper adminMapper, UserMapper userMapper, SubjectMapper subjectMapper) {
        this.adminMapper = adminMapper;
        this.userMapper = userMapper;
        this.subjectMapper = subjectMapper;
    }

    @RequestMapping("/index_login")
    public String login(){
        return "redirect:/login.html";
    }

    @RequestMapping("/loginCL")
    public String loginCL(
                    @RequestParam("username") String username,
                    @RequestParam("pwd") String pwd,
                    HttpSession session,
                    Model model) {
        session.setAttribute("loginUser", username);
        List<Admin> admin = adminMapper.findById(username);
        List<User> user = userMapper.findById(username);
        if (admin.size() > 0) {
            if(admin.get(0).getAdminId().trim().equals(username.trim()) && admin.get(0).getPassword().trim().equals(pwd.trim())) {
                session.setAttribute("object", admin.get(0));
                if (admin.get(0).getLevel().trim().equals("SuperAdmin")) {
                    session.setAttribute("level", "超级管理员");
                } else {
                    session.setAttribute("level", "管理员");
                    session.setAttribute("l1", String.valueOf(admin.get(0).getPower().charAt(0)));
                    session.setAttribute("l2", String.valueOf(admin.get(0).getPower().charAt(1)));
                    session.setAttribute("l3", String.valueOf(admin.get(0).getPower().charAt(2)));
                    session.setAttribute("l4", String.valueOf(admin.get(0).getPower().charAt(3)));
                }
                return "redirect:/main.html";
            }
        } else if(user.size() > 0) {
            if(user.get(0).getUserId().trim().equals(username.trim()) && user.get(0).getPassword().trim().equals(pwd.trim())) {
                if (user.get(0).getUserType() == 0) {
                    session.setAttribute("user", user.get(0));
                    session.setAttribute("level", "学员");
                } else {
                    List<Subject> subjects = subjectMapper.findById(user.get(0).getSubjectId().trim());
                    if (subjects.size() > 0) {
                        user.get(0).setSubjectId(subjects.get(0).getSubjectName());
                    }
                    session.setAttribute("user", user.get(0));
                    session.setAttribute("level", "教练");
                }
                return "redirect:/main.html";
            }
        }
        model.addAttribute("msg", "用户不存在或密码错误！！！");
        return "/pages-signin.html";
    }

    @RequestMapping("/sign_out")
    public String signOut(HttpSession session) {

        session.removeAttribute("loginUser");
        return "redirect:/";

    }

    @RequestMapping("/sign_recover")
    public String signRecover(
            @RequestParam("id") String id,
            @RequestParam("card") String card,
            @RequestParam("phone") String phone,
            @RequestParam("password") String password,
            Model model) {

        System.out.println(id);
        System.out.println(id == null);
        if (id == null || id.trim().equals("")) {
            model.addAttribute("msg", "请输入账号！！！");
            return "/pages-recover-password.html";
        }
        Admin admin = adminMapper.findById(id).size() <= 0 ? null : adminMapper.findById(id).get(0);
        User user = userMapper.findById(id).size() <= 0 ? null : userMapper.findById(id).get(0);
        boolean flag = false;
        if (admin == null && user == null) {
            model.addAttribute("msg", "账号不存在！！！");
        } else if (user == null){
            if (card.trim().equals(admin.getCard().trim()) && phone.trim().equals(admin.getPhone().trim())) {
                if (password.length() >= 6 && password.length() <= 16) {
                    admin.setPassword(password);
                    adminMapper.update(admin);
                    model.addAttribute("msg", "密码重置成功！！！");
                } else {
                    model.addAttribute("msg", "密码的长度应在6~16之间！！！");
                    flag = true;
                }
            } else {
                model.addAttribute("msg", "信息不匹配！！！");
                flag = true;
            }
        } else {
            if (card.trim().equals(user.getCard().trim()) && phone.trim().equals(user.getPhone().trim())) {
                if (password.length() >= 6 && password.length() <= 16) {
                    user.setPassword(password);
                    userMapper.update(user);
                    model.addAttribute("msg", "密码重置成功！！！");
                } else {
                    model.addAttribute("msg", "密码的长度应在6~16之间！！！");
                    flag = true;
                }
            } else {
                model.addAttribute("msg", "信息不匹配！！！");
                flag = true;
            }
        }
        if (flag) {
            model.addAttribute("id", id);
            model.addAttribute("card", card);
            model.addAttribute("phone", phone);
        }

        return "/pages-recover-password.html";
    }

}
